package Cau11;

import java.awt.EventQueue;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.net.MalformedURLException;
import java.rmi.Naming;
import java.rmi.NotBoundException;
import java.rmi.RemoteException;

import javax.swing.JFrame;
import javax.swing.JTextField;
import javax.swing.JTextArea;
import javax.swing.JButton;
import javax.swing.JLabel;
import java.awt.Font;


public class ClientStr implements ActionListener {

	private JFrame frame;
	private JTextField txtinput;
	JTextArea txtkq;
	JButton btnUpper,btninsert,btnDel,btnLower ;
	JTextField txtinsert;
	JTextField txtpos;
	JTextField txtd;
	JTextField txtc;
	JLabel lblinput,lblinsert,lblpos,lbldau,lblcuoi;
	/**
	 * Launch the application.
	 */
	public static void main(String[] args) {
		EventQueue.invokeLater(new Runnable() {
			public void run() {
				try {
					ClientStr window = new ClientStr();
					window.frame.setVisible(true);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
	}

	/**
	 * Create the application.
	 */
	public ClientStr() {
		initialize();
	}

	/**
	 * Initialize the contents of the frame.
	 */
	private void initialize() {
		frame = new JFrame("Bai 10_Client");
		frame.setResizable(false);
		frame.setBounds(100, 100, 450, 364);
		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		frame.getContentPane().setLayout(null);
		
		txtinput = new JTextField();
		txtinput.setBounds(76, 106, 337, 27);
		frame.getContentPane().add(txtinput);
		txtinput.setColumns(10);
		
		txtkq = new JTextArea();
		txtkq.setEditable(false);
		txtkq.setBounds(10, 11, 403, 84);
		frame.getContentPane().add(txtkq);
		
		btnUpper = new JButton("Upper");
		btnUpper.addActionListener(this);
		btnUpper.setBounds(10, 252, 89, 63);
		frame.getContentPane().add(btnUpper);
		
		btnLower = new JButton("Lower");
		btnLower.addActionListener(this);
		btnLower.setBounds(116, 252, 89, 63);
		frame.getContentPane().add(btnLower);
		
		lblinput = new JLabel("Input");
		lblinput.setFont(new Font("Tahoma", Font.BOLD, 15));
		lblinput.setBounds(20, 110, 46, 14);
		frame.getContentPane().add(lblinput);
		
		lblinsert = new JLabel("IString");
		lblinsert.setFont(new Font("Tahoma", Font.BOLD, 15));
		lblinsert.setBounds(21, 146, 59, 19);
		frame.getContentPane().add(lblinsert);
		
		txtinsert = new JTextField();
		txtinsert.setColumns(10);
		txtinsert.setBounds(76, 144, 337, 27);
		frame.getContentPane().add(txtinsert);
		
		lblpos = new JLabel("Position");
		lblpos.setFont(new Font("Tahoma", Font.BOLD, 15));
		lblpos.setBounds(20, 184, 66, 19);
		frame.getContentPane().add(lblpos);
		
		txtpos = new JTextField();
		txtpos.setColumns(10);
		txtpos.setBounds(86, 182, 51, 27);
		frame.getContentPane().add(txtpos);
		
		lbldau = new JLabel("From");
		lbldau.setFont(new Font("Tahoma", Font.BOLD, 15));
		lbldau.setBounds(177, 190, 46, 19);
		frame.getContentPane().add(lbldau);
		
		txtd = new JTextField();
		txtd.setColumns(10);
		txtd.setBounds(224, 185, 51, 27);
		frame.getContentPane().add(txtd);
		
		lblcuoi = new JLabel("To");
		lblcuoi.setFont(new Font("Tahoma", Font.BOLD, 15));
		lblcuoi.setBounds(312, 190, 30, 19);
		frame.getContentPane().add(lblcuoi);
		
		txtc = new JTextField();
		txtc.setColumns(10);
		txtc.setBounds(351, 185, 51, 27);
		frame.getContentPane().add(txtc);
		
		btninsert = new JButton("Insert");
		btninsert.addActionListener(this);
		btninsert.setBounds(215, 252, 89, 63);
		frame.getContentPane().add(btninsert);
		
		btnDel = new JButton("Delete");
		btnDel.addActionListener(this);
		btnDel.setBounds(324, 252, 89, 63);
		frame.getContentPane().add(btnDel);
	}

	@Override
	public void actionPerformed(ActionEvent e) {

		String s=txtinput.getText();
		if(e.getSource()==btnUpper){	
			try {
				ProcessString ps=(ProcessString)Naming.lookup("rmi://localhost/ServerStr");
				txtkq.setText(ps.Upper(s));
			} catch (MalformedURLException e1) {
				e1.printStackTrace();
			} catch (RemoteException e1) {
				e1.printStackTrace();
			} catch (NotBoundException e1) {
				e1.printStackTrace();
			}
		}
		if(e.getSource()==btnLower){
			try {
				ProcessString ps=(ProcessString)Naming.lookup("rmi://localhost/ServerStr");
				txtkq.setText(ps.Lower(s));
			} catch (MalformedURLException e1) {
				e1.printStackTrace();
			} catch (RemoteException e1) {
				e1.printStackTrace();
			} catch (NotBoundException e1) {

				e1.printStackTrace();
			}
		}
		if(e.getSource()==btninsert){
			
			try {
				ProcessString ps=(ProcessString)Naming.lookup("rmi://localhost/ServerStr");
				int pos=Integer.parseInt(txtpos.getText());
				txtkq.setText(ps.Insert(s, txtinsert.getText(), pos));
			} catch (MalformedURLException e1) {
				// TODO Auto-generated catch block
				e1.printStackTrace();
			} catch (RemoteException e1) {
				// TODO Auto-generated catch block
				e1.printStackTrace();
			} catch (NotBoundException e1) {
				// TODO Auto-generated catch block
				e1.printStackTrace();
			}
		}
		if(e.getSource()==btnDel){	
			try {
				ProcessString ps=(ProcessString)Naming.lookup("rmi://localhost/ServerStr");
				int posd=Integer.parseInt(txtd.getText());
				int posc=Integer.parseInt(txtc.getText());
				txtkq.setText(ps.Delete(s, posd, posc));
			} catch (MalformedURLException e1) {
				// TODO Auto-generated catch block
				e1.printStackTrace();
			} catch (RemoteException e1) {
				// TODO Auto-generated catch block
				e1.printStackTrace();
			} catch (NotBoundException e1) {
				// TODO Auto-generated catch block
				e1.printStackTrace();
			}
		}
	}
}
